package com.pt.manage.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.pt.common.annotation.Log;
import com.pt.common.core.controller.BaseController;
import com.pt.common.core.domain.AjaxResult;
import com.pt.common.enums.BusinessType;
import com.pt.manage.domain.studentHomework;
import com.pt.manage.service.IstudentHomeworkService;
import com.pt.common.utils.poi.ExcelUtil;
import com.pt.common.core.page.TableDataInfo;

/**
 * 学生作业Controller
 * 
 * @author ruoyi
 * @date 2025-02-28
 */
@RestController
@RequestMapping("/manage/studentHomework")
public class StudentHomeworkController extends BaseController
{
    @Autowired
    private IstudentHomeworkService studentHomeworkService;
    @Autowired
    private ResourceLoader resourceLoader;
    @Value("${ruoyi.filePath}")
    private String sysFilePath;
    /**
     * 查询学生作业列表
     */

    @GetMapping("/list")
    public TableDataInfo list(studentHomework studentHomework)
    {
        startPage();

        List<studentHomework> list = studentHomeworkService.selectstudentHomeworkList(studentHomework);
        return getDataTable(list);
    }

    /**
     * 导出学生作业列表
     */
    @Log(title = "学生作业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, studentHomework studentHomework)
    {
        List<studentHomework> list = studentHomeworkService.selectstudentHomeworkList(studentHomework);
        ExcelUtil<studentHomework> util = new ExcelUtil<studentHomework>(studentHomework.class);
        util.exportExcel(response, list, "学生作业数据");
    }

    /**
     * 获取学生作业详细信息
     */
    @GetMapping(value = "/{studentID}")
    public AjaxResult getInfo(@PathVariable("studentID") Long studentID)
    {
        return success(studentHomeworkService.selectstudentHomeworkByStudentID(studentID));
    }

    /**
     * 新增学生作业
     */
    @Log(title = "学生作业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody studentHomework studentHomework)
    {
        studentHomework.setStatus(2L);
        return toAjax(studentHomeworkService.updatestudentHomework(studentHomework));
    }


    /**
     * 修改学生作业
     */
    @Log(title = "学生作业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody studentHomework studentHomework)
    {
        studentHomework.setStatus(2L);

        return toAjax(studentHomeworkService.updatestudentHomework(studentHomework));
    }

    /**
     * 删除学生作业
     */
    @Log(title = "学生作业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentIDs}")
    public AjaxResult remove(@PathVariable Long[] studentIDs)
    {
        return toAjax(studentHomeworkService.deletestudentHomeworkByStudentIDs(studentIDs));
    }
    @GetMapping("/download")
    public void downloadFile(@RequestParam("url") String fileUrl,HttpServletResponse response) throws IOException {
        // 1. 获取文件
        File file = new File(sysFilePath+fileUrl);

        // 2. 设置响应头
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));

        // 3. 写入响应流
        Files.copy(file.toPath(), response.getOutputStream());
    }

}
